#pragma once

#include "WMIEventSink.h"

#define WMI_HANDLER_RTCODE_OK                                 1
#define WMI_HANDLER_RTCODE_CREATE_LOCATOR_FAILED              2
#define WMI_HANDLER_RTCODE_CONNECT_NAMESPACE_FAILED           3
#define WMI_HANDLER_RTCODE_SET_PROXY_FAILED                   4
#define WMI_HANDLER_RTCODE_EVENTSINK_HAS_BEEN_REGISTERED      5
#define WMI_HANDLER_RTCODE_CREATE_UNSECUREDAPARTMENT_FAILED   6
#define WMI_HANDLER_RTCODE_EXEC_NOTIFYQUERY_FAILED            7
#define WMI_HANDLER_RTCODE_EXEC_QUERY_FAILED                  8
#define WMI_HANDLER_RTCODE_NOT_CONNECTED                      9

class CWMIHandler
{
public:
  CWMIHandler(void);
  virtual ~CWMIHandler(void);
	// If the COM has been initialized, the function will return false
  bool initCOMAndSecurity(DWORD dwCoInit);
  ULONG connectToNamespace(LPCTSTR lpcszWmiNamespace);
  ULONG registerEventSink(LPCTSTR lpcszQuery, CWMIEventSink * pEventSink);
  ULONG unregisterEventSink(CWMIEventSink * pEventSink);
  bool isConnected(void)
  {
    return m_bConnected;
  }
  ULONG execQuery(LPCTSTR lpcszQuery, IEnumWbemClassObject ** ppEnumerator);
protected:
  void cleanup(void);
private:
  IWbemLocator *m_pWbemLocator;
  bool m_bConnected;
  bool m_bNeedUninitCom;
protected:
  IWbemServices *m_pWbemSvc;
};
